package com.binvshe.binvshe.opus;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.account.Account;
import com.binvshe.binvshe.account.ui.LoginActivity;
import com.binvshe.binvshe.app.home.adapter.MediaType;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.common.image.IML;
import com.binvshe.binvshe.entity.HomeEntity;
import com.binvshe.binvshe.mine.view.CommonTextView;
import com.binvshe.binvshe.mine.view.ThumbUpView;
import com.binvshe.binvshe.organization.tag.TagBean;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.frameworkbase.utils.DateTimeUtil;

import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

import static com.binvshe.binvshe.R.id.mTvContent;
import static com.binvshe.binvshe.R.id.tvContent1;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2017/1/16
 */
@SuppressLint("ValidFragment")
public class StoryTypeFragment extends BaseFragment implements ThumbUpView.OnThumbUp {
    HomeEntity mData;
    @Bind(R.id.civUserHead)
    CircleImageView mCivUserHead;
    @Bind(R.id.tvUserName)
    TextView mTvUserName;
    @Bind(R.id.tvOrgName)
    TextView mTvOrgName;
    @Bind(R.id.ivCover)
    ImageView mIvCover;
    @Bind(R.id.tvOpusTitle)
    TextView mTvOpusTitle;
    @Bind(R.id.tuvCollection)
    ThumbUpView mTuvCollection;
    @Bind(R.id.tvOriginal)
    TextView mTvOriginal;
    @Bind(R.id.tvRole)
    TextView mTvRole;
    @Bind(R.id.tvTag)
    TextView mTvTag;
    @Bind(R.id.tvDesc)
    TextView mTvDesc;
    @Bind(R.id.tvContent2)
    TextView mTvContent2;
    @Bind(R.id.tvContent1)
    TextView mTvContent1;
    @Bind(R.id.ctvMenu)
    CommonTextView mCtvMenu;
    @Bind(R.id.ctvReadNew)
    CommonTextView mCtvReadNew;
    @Bind(R.id.linerBtn)
    LinearLayout mLinerBtn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        initDataView();
        getTagData();
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private StoryTypeFragment() {
    }

    public StoryTypeFragment(HomeEntity data) {
        mData = data;
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_opus_story, container, false);
    }

    private void initDataView() {
        Account user = mData.getUser();
        mTvUserName.setText(user.getNickName());
        IML.load(getContext(), mCivUserHead, user.getPortrait());
        mTvOrgName.setText(user.getInClub() ? user.getClub().getName() : "");

        if (mData.isFavorite()) {
            mTuvCollection.setLike();
        } else {
            mTuvCollection.setUnlike();
        }
        mTuvCollection.setOnThumbUp(this);

        IML.load(getContext(), mIvCover, mData.getCover_image());
        mTvOpusTitle.setText(mData.getTitle());
        mTvDesc.setText("小说简介 " + mData.getDescription());
        if (TextUtils.equals(MediaType.LONG_STORY, mData.getMedia_type())) {//长篇小说
            mLinerBtn.setVisibility(View.VISIBLE);
            mTvContent2.setVisibility(View.GONE);
            mTvContent1.setVisibility(View.GONE);
            mCtvMenu.setLeftBottomTextString("更新于" + DateTimeUtil.getDateStr(new Date(mData.getUpdate_time())));
            mCtvReadNew.setLeftBottomTextString("连载至" + mData.getContent());
        } else {//单篇处理
            mLinerBtn.setVisibility(View.GONE);
            mTvContent2.setVisibility(View.VISIBLE);
            Log.e("mData.getContent()",mData.getContent());
            mTvContent1.setText(mData.getContent());//存放单篇的内容
//            mTvContent.setText(mData.getContent());
        }
    }

    @OnClick({R.id.ctvMenu, R.id.ctvReadNew})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ctvMenu:
                com.binvshe.binvshe.opus.LongStoryMenuActivity.start(getContext(), mData.getId(), mData.getTitle());

                break;
            case R.id.ctvReadNew:
                com.binvshe.binvshe.opus.StoryDetailsActivity.start(getContext(),mData.getId(),mData.getTitle());
                break;

        }
    }

    /**
     * 收藏
     */
    private void addFavorite() {
        IHttpRequest request = new MyHttpRequest(getContext());
        String url = String.format(UrlCenter.EVENT_FAVORITE, "article", mData.getId());
        request.post(url, new UrlParams(), new DataRequestListener(String.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(Object o) {
                mTuvCollection.setLike();
                Toast.makeText(getContext(), "收藏成功", Toast.LENGTH_SHORT).show();
                super.onSuccess(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                Toast.makeText(getContext(), strMsg, Toast.LENGTH_SHORT).show();
                LoginActivity.start(getActivity());
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    /**
     * 取消收藏
     */
    private void deleteFavorite() {
        IHttpRequest request = new MyHttpRequest(getContext());
        String url = String.format(UrlCenter.EVENT_FAVORITE, "article", mData.getId());
        request.delete(url, new UrlParams(), new DataRequestListener(String.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(Object o) {
                mTuvCollection.setUnlike();
                Toast.makeText(getContext(), "取消收藏", Toast.LENGTH_SHORT).show();
                super.onSuccess(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                LoginActivity.start(getActivity());
                Toast.makeText(getContext(), strMsg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void like(boolean like) {
        if (like) {
            addFavorite();
        } else {
            deleteFavorite();
        }
    }

    public void getTagData() {
        IHttpRequest request = new MyHttpRequest(getContext());
        String url = String.format(UrlCenter.GET_ART_TAG, mData.getId());
        request.get(url, null, new DataRequestListener<List<TagBean>>(TagBean.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(List<TagBean> o) {
                super.onSuccess(o);
                String original = "";
                String role = "";
                String tag = "";
                mTvOriginal.setVisibility(View.GONE);
                if (o != null) {
                    for (TagBean tagBean : o) {
                        switch (tagBean.getTagType()) {
                            case 0://原创
                                original += tagBean.getTagContent() + " ";
                                mTvOriginal.setVisibility(View.VISIBLE);
                                break;
                            case 1://角色
                                role += tagBean.getTagContent() + " ";
                                break;
                            case 2://分类标签
                                tag += tagBean.getTagContent() + " ";
                                break;
                        }
                    }
                }
                mTvOriginal.setText(Html.fromHtml(getString(R.string.originalText, original)));
                mTvRole.setText(Html.fromHtml(getString(R.string.roleText, role)));
                mTvTag.setText(Html.fromHtml(getString(R.string.typeText, tag)));

            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });

    }

}
